<?php
namespace App\Servers\Backend;

use App\Repositories\Backend\CommentRepository;
use App\Repositories\Backend\ComboParameterRepository;
use App\Repositories\Backend\ComboBranchRepository;

class CommentServer extends CommonServer
{

    public function __construct(
        CommentRepository $commentRepository,
        ComboParameterRepository $comboparameterRepository,
        ComboBranchRepository $combobranchRepository
    ) {
        $this->commentRepository = $commentRepository;
        $this->comboparameterRepository = $comboparameterRepository;
        $this->combobranchRepository = $combobranchRepository;
    }

    /**
     * 列表
     * @param  Array $input [search]
     * @return Array
     */
    public function index($input)
    {

        $data = $this->commentRepository->getLists($input)->toArray();

        foreach ($data['data'] as $k=>$v){
            if(!$v['doctor']){
                $data['data'][$k]['doctor'] ='全部';
            }else{
                $data['data'][$k]['doctor'] = $v['doctor'][0]['doctor_name'];
            }
        }

        $result['lists']   = $data;

        return ['获取成功', $result];
    }

    /**
     * 新增
     * @param  Array $input [username, userimage, likenumber, content, status]
     * @return Array
     */
    public function create($input)
    {
        $user_name      = isset($input['username']) ? strval($input['username']) : '';
        $user_image     = isset($input['userimage']) ? strval($input['userimage']) : '';
        $like_number    = isset($input['likenumber']) ? intval($input['likenumber']) : '';
        $content        = isset($input['content']) ? strval($input['content']) : '';
        $model_id         = isset($input['model_id']) ? intval($input['model_id']) : 0;


        if (!$user_name || !$user_image || !$like_number || !$content) {
            return ['code' => ['x00004', 'system']];
        }

        $result['list'] = $this->commentRepository->create($user_name, $user_image, $like_number, $content, $model_id);

        if (!$result['list']) {
            return ['code' => ['x00001', 'system']];
        }

        return ['新增成功', $result];
    }
//
//    /**
//     * 编辑
//     * @param  Array $input [username, email, password, permission_id, status]
//     * @param  Int $id
//     * @return Array
//     */
//    public function update($id, $input)
//    {
//        $username      = isset($input['username']) ? strval($input['username']) : '';
//        $email         = isset($input['email']) ? strval($input['email']) : '';
//        $password      = (isset($input['password']) && !empty($input['password'])) ? Hash::make(strval($input['password'])) : '';
//        $permission_id = isset($input['permission_id']) ? intval($input['permission_id']) : 0;
//        $status        = isset($input['status']) ? intval($input['status']) : 0;
//
//        if (!$username || !$email || !$permission_id) {
//            return ['code' => ['x00004', 'system']];
//        }
//
//        // 用户名和邮箱重复判断
//        $search_where = [
//            'filter' => ['username', 'email'],
//            'search' => [
//                'username' => $username,
//                'email'    => ['or', $email],
//                'id'       => ['!=', $id],
//            ],
//        ];
//        $list = $this->adminRepository->getList($search_where);
//        if (!empty($list)) {
//            $code = $list->username == $username ? 'x00001' : 'x00002';
//            return ['code' => [$code, 'system']];
//        }
//
//        $result = $this->adminRepository->update($id, $username, $email, $password, $permission_id, $status);
//
//        if (!$result) {
//            return ['code' => ['x00001', 'system']];
//        }
//        return ['更新成功', $result];
//    }
//
    /**
     * 删除
     * @param  Int $id
     * @return Array
     */
    public function destroy($id)
    {
        $result = $this->commentRepository->destroy($id);

        if (!$result) {
            return ['code' => ['x00002', 'system']];
        }

        return ['删除成功', $result];
    }


    /**
     * 新增/编辑参数
     * @param  Array $input [domains[], combo_id]
     * @return Array
     */
    public function parameter($input)
    {
        $domains   = isset($input['domains']) ? $input['domains'] : '';
        $combo_id  = isset($input['combo_id']) ? intval($input['combo_id']) : '';


        if (!$domains || !$combo_id) {
            return ['code' => ['x00004', 'system']];
        }

        $result['list'] = $this->comboparameterRepository->parameter($domains, $combo_id);

        if (!$result['list']) {
            return ['code' => ['x00001', 'system']];
        }

        return ['新增成功', $result];
    }


    /**
     * 新增/编辑内容
     * @param  Array $input [domains[], combo_id]
     * @return Array
     */
    public function branch($input)
    {
        $domains   = isset($input['domains']) ? $input['domains'] : '';
        $combo_id  = isset($input['combo_id']) ? intval($input['combo_id']) : '';


        if (!$domains || !$combo_id) {
            return ['code' => ['x00004', 'system']];
        }

        $result['list'] = $this->combobranchRepository->branch($domains, $combo_id);

        if (!$result['list']) {
            return ['code' => ['x00001', 'system']];
        }

        return ['新增成功', $result];
    }





}
